<?php
if ( !defined('CAPSHAW_LENGTH') ) {
	define('CAPSHAW_LENGTH', 'XXXXX'); 
}
if ( !defined('CAPSHAW_FONT_PATH') ) {
	define('CAPSHAW_FONT_PATH', INSTALL_DIR . 'fonts/');
}
if ( !defined('CAPSHAW_FONT') ) {
	define('CAPSHAW_FONT', 'verdana14');
}

class Capshaw
{
	/*******************************
	 * Output code image
	 */
	static public function draw($code) {
		switch( CAPSHAW_FONT ) {
                case 'comicsans14': 
			define('CAPSHAW_XOFFSET',  6);
                        define('CAPSHAW_YOFFSET',  4);
                        define('CAPSHAW_SPACE',    9);
                        define('CAPSHAW_WIDTH',    23);
                        define('CAPSHAW_HEIGHT',   24);
			break;

		case 'verdana14':
		default:
			define('CAPSHAW_XOFFSET',  6);
			define('CAPSHAW_YOFFSET',  4);
			define('CAPSHAW_SPACE',    9);
			define('CAPSHAW_WIDTH',    23);
			define('CAPSHAW_HEIGHT',   24);
			break;
		}

		//Dimensions
		$length = strlen($code);
		$height = CAPSHAW_HEIGHT;
		$width  = $length * CAPSHAW_WIDTH;

		//Images
		$im = imagecreate( $width, $height );
		$tmpl = imagecreatefrompng( CAPSHAW_FONT_PATH . CAPSHAW_FONT .'.png');

		//Colours
		$trans = imagecolorallocate( $im, 255,   0,   0);
		$white = imagecolorallocate( $im, 255, 255, 255);
		$black = imagecolorallocate( $im,   0,   0,   0);

		$colours[0] = imagecolorallocate( $im, 0xCC,    0,    0);
		$colours[1] = imagecolorallocate( $im, 0xFF, 0x99,    0);
		$colours[2] = imagecolorallocate( $im, 0xFF, 0xFF,    0);
		$colours[3] = imagecolorallocate( $im,    0, 0x99,    0);
		$colours[4] = imagecolorallocate( $im,    0,    0, 0xCC);
		$colours[5] = imagecolorallocate( $im, 0x66,    0, 0x66);

		for( $i=0; $i < count($colours); $i++ ) {
			$cw = mt_rand( 5, 40);
			imagefilledellipse ( $im, mt_rand( 0, $width), mt_rand( 0, $height), $cw, $cw, $colours[$i]);
		}


		//Create Phrase image from template
		for( $i=0; $i < strlen($code); $i++ ) {
		        $c = ord( $code{$i} );

		        if ( $c >= 48 && $c <= 57 ) {
                		$c = $c - 48;
		        } else {
                		$c = $c - 55;
        		}
		        $x = CAPSHAW_XOFFSET + ( $c * ( CAPSHAW_WIDTH + CAPSHAW_SPACE ) );

			imagecopyresized( $im, $tmpl, $i*CAPSHAW_WIDTH, mt_rand(0,5)-3, 
								           $x, CAPSHAW_YOFFSET, 
							 CAPSHAW_WIDTH, CAPSHAW_HEIGHT, 
							 CAPSHAW_WIDTH, CAPSHAW_HEIGHT);
		}


		/*
                // Lines
		$dx = floor($width/10);
		$dy = floor($height/4);
                for( $i=0; $i < $height; $i+=$dy ) {
                        $y = $i + ($dy / 2);
                        imageline( $im, 0, $y, $width, $y, $white);
                }
                for( $i=0; $i < $width; $i+=$dx ) {
                        $x = $i + ($dx / 2);
                        imageline( $im, $x, 0, $x, $height, $white);
                }
		*/
		
		
		//Output image
		imagecolorset( $im, $white, 0, 0, 0);
		imagecolortransparent( $im, $trans );
		header("Content-type: image/png");
		imagepng( $im );
		imagedestroy( $im );
		imagedestroy( $tmpl );
	}
} 
 
?>
